package com.appsspot.server.parser;

import java.io.File;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;

import com.appsspot.client.parcer.ConfigClient;
import com.appsspot.client.parcer.SubTab;
import com.appsspot.client.parcer.SubTabElements;
import com.appsspot.client.parcer.Tab;
import com.appsspot.client.parcer.Element;

public class SAXPars extends DefaultHandler {

	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (qName == "tab") {
			String tabName = atts.getValue("name");
			Tab tab = new Tab(tabName);
			ConfigClient.getInstants().setTab(tab);
		} else if (qName == "subtab") {
			Tab tab = ConfigClient.getInstants().getLastTab();
			String subTabName = atts.getValue("name");
			String subTabURL = atts.getValue("url");
			SubTab subtab = new SubTab(subTabURL, subTabName);
			tab.setSubTab(subtab);
			ConfigClient.getInstants().setSubTabElements(new SubTabElements(subtab));
		} else if (qName == "element") {
			SubTabElements subTabElements = ConfigClient.getInstants().getLastSubTabElements();
			String nameUrl = atts.getValue("nameUrl");
			String imageUrl = atts.getValue("imageUrl");
			String codId = atts.getValue("codId");
			String about = atts.getValue("about");
			Element el = new Element(nameUrl, imageUrl, codId, about);
			subTabElements.getArrElements().add(el);		
		} else if (qName == "topToolStrip") {
			String link[] = new String[2]; 
			link[0] = atts.getValue("name");
			link[1] = atts.getValue("url");
			ConfigClient.getInstants().setTopToolStripLink(link);
		}
	}

	public static void parsing(String fileName) throws Exception {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		SAXPars saxp = new SAXPars();

		parser.parse(new File(fileName), saxp);
	}
}
